package com.tianzj.mvc.system.service.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tianzj.mvc.system.dao.SysRoleMenuMapper;
import com.tianzj.mvc.system.pojo.SysRoleMenu;
import com.tianzj.mvc.system.pojo.SysRoleMenuExample;
import com.tianzj.mvc.system.service.SysRoleMenuService;
import com.tianzj.util.SnowflakeIdWorker;

@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

	@Autowired
	private SysRoleMenuMapper sysRoleMenuMapper;
	
	@Override
	public void deleteByRoleId(Long roleId) {
	     sysRoleMenuMapper.deleteByRoleId(roleId);
	}

	@Override
	public void saveRoleMenu(Long roleId, String menuIds) {
		String[] menuIdsArray = menuIds.split(",");
		for (String menuId : menuIdsArray) {
			SysRoleMenu sysRoleMenu = new SysRoleMenu(); 
			sysRoleMenu.setId(new SnowflakeIdWorker(0, 0).nextId());
			sysRoleMenu.setMenuId(Long.parseLong(menuId));
			sysRoleMenu.setRoleId(roleId);
			sysRoleMenu.setStatus(1L);
			sysRoleMenuMapper.insertSelective(sysRoleMenu);
		}
	}

	@Override
	public List<SysRoleMenu> getRoleMenuByRoleId(Long roleId) {
		SysRoleMenuExample example = new SysRoleMenuExample();
		SysRoleMenuExample.Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		return sysRoleMenuMapper.selectByExample(example);
	}

}
